<?php
/**
 * Created by PhpStorm.
 * User: yui
 * Date: 2017/4/4
 * Time: 18:52
 */

namespace api\controllers;

use Yii;
use common\controllers\ApiCommonCtrl;
use common\services\CartSve;


class CartController extends ApiCommonCtrl
{

    /**
     * 获取购物车列表
     */
    public function actionList()
    {
        $this->checkLogin();
        $CartSve = new CartSve();
        $rs = $CartSve->getCartList($this->TokenObj);
        return $rs;
    }

    /**
     * 添加商品
     * goods_list=1:1,3:1
     */
    public function actionAdd()
    {
        $this->checkLogin();
        $goodsList = Yii::$app->request->get('goods_list', '');
        $CartSve = new CartSve();
        $CartSve->addGoods($goodsList, $this->TokenObj);
    }


    /**
     * 更新购物车
     * goods_list=1:2,3:2
     */
    public function actionUpdate()
    {
        $this->checkLogin();
        $goodsList = Yii::$app->request->get('goods_list', '');
        $CartSve = new CartSve();
        return $CartSve->updateCart($goodsList, $this->TokenObj);
    }

    /**
     * 删除商品
     * 可以删除一个或多个
     * goods_ids=1,2,3
     */
    public function actionRemove()
    {
        $this->checkLogin();
        $goodsIds = Yii::$app->request->get('goods_ids');
        $CartSve = new CartSve();
        return $CartSve->removeGoods($goodsIds, $this->TokenObj);
    }

    /**
     * 清空购物车
     * @return array
     */
    public function actionClear()
    {
        $this->checkLogin();
        $CartSve = new CartSve();
        return $CartSve->clear($this->TokenObj);
    }

}